//
//  LoginViewController.m
//  OralRobot
//
//  Created by 王桂敏 on 16/4/7.
//  Copyright © 2016年 huayingdai. All rights reserved.
//

#import "LoginViewController.h"
#import "UserCenterTextField.h"
#import "ForgotPasswordViewController.h"
#import "UINavigationBar+Transparency.h"
//#import "JPUSHService.h"
//#import "UserNameModel.h"
#import "ListView.h"
#import "UMMobClick/MobClick.h"


@interface LoginViewController ()<UITextFieldDelegate,UserCenterTextFieldDelegate>

@property (nonatomic,strong) UIImageView *bgImageView;//  背景图

@property (nonatomic,strong) UserCenterTextField *userNameTF;// 手机号

@property (nonatomic,strong) UserCenterTextField *passwordTF;// 密码

@property (nonatomic,strong) UIButton *retrievePasswordButton;//    找回密码

@property (nonatomic,strong) UIButton *loginButton;//   登录

@end

@implementation LoginViewController

#pragma mark - init -
- (void)initView{
    
    //  背景图
    self.bgImageView = [UIImageView imageViewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) image:@"icon_bgo"];
    [self.view addSubview:self.bgImageView];
    
    //  手机号码
    self.userNameTF = [[UserCenterTextField alloc] initWithFrame:CGRectMake(w(40), h(391), kScreenWidth - w(40) * 2, h(100)) image:nil placeholder:@"请输入账号或手机号" keyboardType:UIKeyboardTypeDefault clearButtonMode:UITextFieldViewModeWhileEditing delegate:self userCenterTextFieldDelegate:self];
    [self.view addSubview:self.userNameTF];
   
    //  密码
    self.passwordTF = [[UserCenterTextField alloc] initWithFrame:CGRectMake(self.userNameTF.x, getMaxY(self.userNameTF) + h(30), self.userNameTF.width, self.userNameTF.height) image:nil placeholder:@"请输入登录密码" keyboardType:UIKeyboardTypeDefault clearButtonMode:UITextFieldViewModeWhileEditing delegate:self userCenterTextFieldDelegate:self];
    self.passwordTF.isPassword = YES;
    self.passwordTF.secureTextEntry = YES;
    [self.view addSubview:self.passwordTF];

    
    //  忘记密码按钮
    self.retrievePasswordButton = [UIButton buttonOfTitleForNormalWithTitle:@"忘记密码?" frame:CGRectMake(getMaxX(self.passwordTF) - w(125),getMaxY(self.passwordTF),w(125),h(22) + h(19) * 2) target:self action:@selector(handleRetrievePasswordButtonAction:) titleColor:ColorWithHexString(@"a0c0d2") backgroudColor:[UIColor clearColor] font:font(22)];
    self.retrievePasswordButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.view addSubview:self.retrievePasswordButton];
    
    //  登录按钮
    self.loginButton = [UIButton buttonOfImageForNormalWithImage:@"" frame:CGRectMake((kScreenWidth - w(560)) / 2,getMaxY(self.passwordTF) + h(311),self.passwordTF.width,h(90)) target:self action:@selector(handleLoginButtonAction:)];
    [self.loginButton setBackgroundImage:Image(@"btn_login") forState:UIControlStateNormal];
    self.loginButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.view addSubview:self.loginButton];
    
}

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    self.edgesForExtendedLayout = UIRectEdgeTop;//  从导航栏底部开始布局
    
    [self.navigationController.navigationBar setNavigationBarTransparent:YES];

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setNavigationBarTransparent:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self initView];
}

#pragma mark - buttonAction -

- (void)handleRetrievePasswordButtonAction:(UIButton *)sender{
    ForgotPasswordViewController *VC = [[ForgotPasswordViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)handleLoginButtonAction:(UIButton *)sender{

    if (!sender.isSelected) {
        
        if (!self.userNameTF.text.length) {
            
            [self showWarningWithStatus:@"请检查账号或手机号是否符合要求"];
            
        } else if (![self.passwordTF.text isPassword]){
            
            [self showWarningWithStatus:@"请检查密码是否符合要求"];
        
        }
        
    } else {
       
            [self initNetWork];
        
    }
    
}

#pragma mark - UserCenterTextFieldDelegate -
- (void)textFieldDidChange:(UserCenterTextField *)textField{
    
    if (self.userNameTF.text.length && [self.passwordTF.text isPassword]) {
        self.loginButton.selected = YES;
    } else {
        self.loginButton.selected = NO;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - touchesBegan -
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

}

#pragma mark - networkAndNetworkDelegate -
- (void)initNetWork{
    
    self.params[@"mobile"] = self.userNameTF.text;
    self.params[@"password"] = self.passwordTF.text;
//
//    NSString *pushToken = [JPUSHService registrationID];
//    self.params[@"registrationID"] = pushToken.length ? pushToken : self.registrationID;

    
//    self.params[@"registrationID"] = pushToken.length ? pushToken : @"191e35f7e04563a4fc1";
    [self showIndeterminate];
    [self.manager postWithURL:NET_LOGIN param:self.params delegate:self isEncrypt:YES type:NetWorkingType0];
    
}

- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    switch (type) {
        case NetWorkingType0:
        {
            [MobClick profileSignInWithPUID:responseObject[@"userId"]];
            [self setBackgroundTime:responseObject[@"sysDateTime"]];
            self.beginTime = self.backgroundTime;
            
            //  登录成功
            [self setIsLogin:YES];
            
            
            //  1.保存用户信息
            [self setPhone:responseObject[@"phonenum"]];
            [self setNickName:responseObject[@"username"]];
            [self setPassword:self.passwordTF.text];
            [self setToken:responseObject[@"token"]];
            [self setUserID:responseObject[@"userId"]];
            [self setOpenTypeOfThird:responseObject[@"openType"]];
            //个人信息
            [self setIconURL:responseObject[@"headImg"]];
             //  性别
            [self setGender:responseObject[@"sex"]];
            [self setProvince:responseObject[@"adress"]];
            
            //机构图片
            [self setOrgIconUrl:responseObject[@"picUrl"]];
            //机构名称
            [self setOrgName:responseObject[@"orgname"]];
            //校长姓名
            [self setLegalPerson:responseObject[@"lawperson"]];
            //银行卡号
            [self setCardNum:responseObject[@"cardnum"]];
            //余额
            [self setBalance:responseObject[@"accountbalance"]];
            //机构邮箱
            [self setOrgEmail:responseObject[@"email"]];
            //机构地址
            [self setOrgAdress:responseObject[@"adress"]];

            NSLog(@"%@",self.gender);
            
            //  2.跳转到首页
          
            CustumTabBarController *VC = [[CustumTabBarController alloc] init];
            AppDelegateInstance.window.rootViewController = VC;
            [self postNotificationName:kNotificationIsAnnouncement object:nil];
        }
            break;
            
        default:
            break;
    }

}

- (void)networkingHandlefail:(NetworkingHandle *)handle type:(NetWorkingType)type{
    [self hideHud];
}

- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
   
}




@end
